﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Collections.Generic;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.ZNH.ZMCB.UI.Utils;
using System.Text.RegularExpressions;

namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    /// <summary>
    /// 煤层信息的新增、修改、查看页面
    /// 对应后台ServiceZMCMB10.java
    /// @author 王开兵
    /// </summary>
    /// 

    public partial class PlanWorkday : EFViewCmes
    {
        //private MineBaseInfo _block = new MineBaseInfo();//私有化下拉列表

        public PlanWorkday(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        //public override void Initialize(Platform.Core.EIInfo initData)
        //{
        //    //initData.AddBlock(_block.GetPlanDaysType("cmes.cs.planType"));//列表区
        //    initData.AddBlock(_block.GetYears("result"));//列表区

        //    base.Initialize(initData);
        //}

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            //_view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                //case "detail":
                //    this.Commit.Visibility = Visibility.Collapsed;
                //    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                //    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, System.Windows.RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            //EIRow eiRow = this.grid.EIBlock.GetRow(0);
            //if (ValidateMonth(eiRow))
            //{
            //    return;
            //}

            bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIRow eiRow = this.grid.EIBlock.GetRow(0);
            if (ValidateMonth(eiRow))
            {
                return;
            }
            //EIRow eiRow = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
            //EIRow eiRow = this.grid.EIBlock.GetRow(0);
            //ValidateMonth(eiRow);
            SetTotal(eiRow);

            string _actionMode = Mode.ToString() == null ? "insert" : Mode.ToString().ToLower();
            EIInfo info = new EIInfo();
            info.Set(EIConstants.ServiceNameKey, "ZMCB15");
            info.Set(EIConstants.MethodNameKey, _actionMode);
            info.SetBlock(this.EIDataContext.GetBlock("result"));

            this.Submit(info, DoCommitCompleted);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, System.Windows.RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 根据计划类型来设定项目名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            bool bFuture = e.NewValue.ToString().Equals("1");
            //bool bFuture = this.fdType.Value.ToString().Equals("中长期计划");
            if (!bFuture)
            {
                this.fdItem.eBlockName = "cmes.cs.yearContent";
                this.fdUnit.Value = "天";
            }
            else
            {
                this.fdItem.eBlockName = "cmes.cs.futureContent";
                this.fdUnit.Value = "月";
            }
  
        }

        private void grid_CellEditEnded(object sender, Telerik.Windows.Controls.GridViewCellEditEndedEventArgs e)
        {

        }

        /// <summary>
        /// 设置工作日或工作月的总数
        /// </summary>
        /// <param name="BlockId"></param>
        /// <returns></returns>
        protected void SetTotal(EIRow eiRow)
        {
            //EIRow eiRow = this.grid.EIBlock.GetRow(0);
            string fdFeb = (eiRow["fdFeb"] == null) ? "" : eiRow["fdFeb"].ToString();
            string fdJan = (eiRow["fdJan"] == null) ? "" : eiRow["fdJan"].ToString();
            string fdMar = (eiRow["fdMar"] == null) ? "" : eiRow["fdMar"].ToString();
            string fdApr = (eiRow["fdApr"] == null) ? "" : eiRow["fdApr"].ToString();
            string fdMay = (eiRow["fdMay"] == null) ? "" : eiRow["fdMay"].ToString();
            string fdJun = (eiRow["fdJun"] == null) ? "" : eiRow["fdJun"].ToString();
            string fdJul = (eiRow["fdJul"] == null) ? "" : eiRow["fdJul"].ToString();
            string fdAug = (eiRow["fdAug"] == null) ? "" : eiRow["fdAug"].ToString();
            string fdSep = (eiRow["fdSep"] == null) ? "" : eiRow["fdSep"].ToString();
            string fdOct = (eiRow["fdOct"] == null) ? "" : eiRow["fdOct"].ToString();
            string fdNov = (eiRow["fdNov"] == null) ? "" : eiRow["fdNov"].ToString();
            string fdDec = (eiRow["fdDec"] == null) ? "" : eiRow["fdDec"].ToString();

            if (this.fdType.Value.Equals("2"))
            {
                this.fdTotal.Value = System.Int32.Parse(fdJan) + System.Int32.Parse(fdFeb) + System.Int32.Parse(fdMar) + System.Int32.Parse(fdApr) +
                    System.Int32.Parse(fdMay) + System.Int32.Parse(fdJun) + System.Int32.Parse(fdJul) + System.Int32.Parse(fdAug) +
                    System.Int32.Parse(fdSep) + System.Int32.Parse(fdOct) + System.Int32.Parse(fdNov) + System.Int32.Parse(fdDec);
            }
            else
            {
                int workYear = 0;
                if (fdJan.Equals("工作"))
                    workYear++;
                if (fdFeb.Equals("工作"))
                    workYear++;
                if (fdMar.Equals("工作"))
                    workYear++;
                if (fdApr.Equals("工作"))
                    workYear++;
                if (fdMay.Equals("工作"))
                    workYear++;
                if (fdJun.Equals("工作"))
                    workYear++;
                if (fdJul.Equals("工作"))
                    workYear++;
                if (fdAug.Equals("工作"))
                    workYear++;
                if (fdSep.Equals("工作"))
                    workYear++;
                if (fdOct.Equals("工作"))
                    workYear++;
                if (fdNov.Equals("工作"))
                    workYear++;
                if (fdDec.Equals("工作"))
                    workYear++;
                this.fdTotal.Value = workYear;
            }

        }

        /// <summary>
        /// 验证
        /// </summary>
        public bool ValidateMonth(EIRow eiRow)
        {
            string[] month = new string[12]{"fdJan","fdFeb","fdMar","fdApr","fdMay","fdJun","fdJul","fdAug","fdSep","fdOct","fdNov","fdDec"};
            for (int i = 0; i < 12; i++)
            {
                if ((eiRow[month[i]] == null) || (eiRow[month[i]].ToString().Equals("")))
                {
                    ValidateMessagePopup((i + 1).ToString() + "月份不能为空");
                    this.TempButtonOpen(true);//确定按钮生效
                    return true;
                }
                else
                {
                    if (this.fdType.Value.ToString().Equals("1"))
                    {
                        if ((!eiRow[month[i]].ToString().Equals("工作")) && (!eiRow[month[i]].ToString().Equals("休息")))
                        {
                            ValidateMessagePopup((i + 1).ToString() + "月的值只能输入工作或者休息");
                            this.TempButtonOpen(true);//确定按钮生效
                            return true;
                        }
                    }
                    else
                    {
                        if (Regex.IsMatch(eiRow[month[i]].ToString(), @"^\d{1,2}$"))
                        {
                            //if (int.Parse(eiRow[month[i]].ToString()) < 0 || int.Parse(eiRow[month[i]].ToString()) > 31)
                            //{
                                if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11)
                                {
                                    if(int.Parse(eiRow[month[i]].ToString()) < 0 || int.Parse(eiRow[month[i]].ToString()) > 31)
                                    {
                                        ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-31的数字");
                                        this.TempButtonOpen(true);//确定按钮生效
                                        return true;
                                    }
                                }
                                else if (i == 3 || i == 5 || i == 8 || i == 10)
                                {
                                    if (int.Parse(eiRow[month[i]].ToString()) < 0 || int.Parse(eiRow[month[i]].ToString()) > 30)
                                    {
                                        ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-30的数字");
                                        this.TempButtonOpen(true);//确定按钮生效
                                        return true;
                                    }
                                }
                                else
                                { 
                                    int year = Convert.ToInt32(this.fdYear.Value.ToString());
                                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 10))
                                    {
                                        if (int.Parse(eiRow[month[i]].ToString()) < 0 || int.Parse(eiRow[month[i]].ToString()) > 29)
                                        {
                                            ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-29的数字");
                                            this.TempButtonOpen(true);//确定按钮生效
                                            return true;
                                        }
                                    }
                                    else
                                    {
                                        if (int.Parse(eiRow[month[i]].ToString()) < 0 || int.Parse(eiRow[month[i]].ToString()) > 28)
                                        {
                                            ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-28的数字");
                                            this.TempButtonOpen(true);//确定按钮生效
                                            return true;
                                        }
                                    }
                                }
                                    
                            //}
                        }
                        else
                        {
                            //ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-31的数字");
                            //this.TempButtonOpen(true);//确定按钮生效
                            //return true;
                            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11)
                            {
                                ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-31的数字");
                                this.TempButtonOpen(true);//确定按钮生效
                                return true;
                            }
                            else if (i == 1 || i == 3 || i == 5 || i == 8 || i == 10)
                            {
                                ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-30的数字");
                                this.TempButtonOpen(true);//确定按钮生效
                                return true;
                            }
                            else
                            {
                                int year = Convert.ToInt32(this.fdYear.Value.ToString());
                                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 10))
                                {
                                    ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-29的数字");
                                    this.TempButtonOpen(true);//确定按钮生效
                                    return true;
                                }
                                else
                                {
                                    ValidateMessagePopup((i + 1).ToString() + "月的值只能输入1-28的数字");
                                    this.TempButtonOpen(true);//确定按钮生效
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;

        }
    }
}
